package com.fanfanwang.ffw.app.ui;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.GlyMainFragment;
import com.fanfanwang.ffw.app.base.FfwActivity;
import com.fanfanwang.ffw.app.bean.UserBean;
import com.fanfanwang.ffw.app.db.DBUtil;
import com.fanfanwang.ffw.app.tenant.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

/**
 * 主activity
 * @author lyf
 *
 */
public class HomeActivity extends FragmentActivity {

	public static int count = 0;
	public static HomeActivity act;
	private GlyMainFragment glyFragmentContent;
	public static SlidingMenu leftRightSlidingMenu;
	public int preFragment=0;//上一个fragment是第几个。（0.1.2）
	//	SetFragment mContent;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		act = this;
		AppManager.getAppManager().addActivity(this);
		//	set the Above View
		//	initLeftRightSlidingMenu();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		//	WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_home);
//		Intent intent=getIntent();
//		UserBean userbean=(UserBean) intent.getSerializableExtra("data");
		glyFragmentContent=GlyMainFragment.newInstance();
		//这个是把登陆得到的用户的信息传过去到fragment里面。
//		Bundle bundle=new Bundle();
//		bundle.putSerializable("data", userbean);
//		glyFragmentContent.setArguments(bundle);
		getSupportFragmentManager().beginTransaction().replace(R.id.home_frame, glyFragmentContent).commit();
		//		addUser();
	}

	private void initLeftRightSlidingMenu() {
		//        setBehindContentView(R.layout.main_left_layout);
		FragmentTransaction leftFragementTransaction = getSupportFragmentManager().beginTransaction();
		//		Fragment leftFrag = new LeftSlidingMenuFragment();
		//		leftFragementTransaction.replace(R.id.main_left_fragment, leftFrag);
		leftFragementTransaction.commit();
		// customize the SlidingMenu
		//        leftRightSlidingMenu = getSlidingMenu();
		leftRightSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);// 设置是左滑还是右滑，还是左右都可以滑，我这里只做了左滑
		leftRightSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// 设置菜单宽度,这个是菜单剩余的部分的宽度
		leftRightSlidingMenu.setFadeDegree(0.35f);// 设置淡入淡出的比例
		leftRightSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);//设置手势模式
		leftRightSlidingMenu.setShadowDrawable(R.drawable.shadow);// 设置左菜单阴影图片
		leftRightSlidingMenu.setFadeEnabled(true);// 设置滑动时菜单的是否淡入淡出
		leftRightSlidingMenu.setBehindScrollScale(0.333f);// 设置滑动时拖拽效果

		//		leftRightSlidingMenu.setSecondaryMenu(R.layout.main_right_layout);
		FragmentTransaction rightFragementTransaction = getSupportFragmentManager().beginTransaction();
		//		Fragment rightFrag = new RightSlidingMenuFragment();
		//		leftFragementTransaction.replace(R.id.main_right_fragment, rightFrag);
		rightFragementTransaction.commit();
	}

	//	private void addUser(){
	//		UserBean bean = new UserBean();
	//		bean.userId = "1";
	//		bean.userName = "陈晓峰";
	//		DBUtil.addUserBean(this, bean);
	//	}
	public static HomeActivity getHomeAct() {
		return act;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		act = null;
	}

	
	@Override
	public void onBackPressed() {
		//	super.onBackPressed();
		if(glyFragmentContent.showFragmentid==preFragment)
		{
			exit();
		}else{
			preFragment=glyFragmentContent.showFragmentid;
			AppContext.exitTime=0;
			exit();
			//			super.onBackPressed();
		}

	}
	public void exit() {
		if ((System.currentTimeMillis() - AppContext.exitTime) > 2000) {
			Toast.makeText(this, getString(R.string.repress_exit),
					Toast.LENGTH_SHORT).show();
			AppContext.exitTime = System.currentTimeMillis();
		} else {
			try {
				com.fanfanwang.ffw.app.AppManager.getAppManager().finishAllActivity();
				ActivityManager activityMgr = (ActivityManager)this
						.getSystemService(Context.ACTIVITY_SERVICE);
				activityMgr.killBackgroundProcesses(this.getPackageName());
				System.exit(0);
			} catch (Exception e) {
				System.exit(0);
			}
		}
	}
}
